﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;

namespace SocketSender
{
    class Program
    {
        static void Main(string[] args)
        {
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            Console.WriteLine("Input port:");
            int port = int.Parse(Console.ReadLine());
            int cnt = 0;
            Socket client = null;
            while (true)
            {
                try
                {
                    if (client == null || client.Connected == false)
                    {
                        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        IPEndPoint ep = new IPEndPoint(ip, port);
                        socket.Bind(ep);
                        socket.Listen(20);
                        Console.WriteLine("Waiting for a new client");
                        client = socket.Accept();
                        socket.Close();
                    }
                    Console.WriteLine("We are Sending data");
                    if (cnt % 3 == 0)
                    {
                        client.Send(Encoding.ASCII.GetBytes("dear mxy"));
                    }
                    else if (cnt % 3 == 1)
                    {
                        client.Send(Encoding.ASCII.GetBytes("dear gyx"));
                    }
                    else if (cnt % 3 == 2)
                    {
                        client.Send(Encoding.ASCII.GetBytes("dear lzx"));
                    }
                    ++cnt;
                    Thread.Sleep(1000);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    Thread.Sleep(1000);
                }
            }
        }
    }
}
